package com.example.administrator.maizuofilm.movie.fragment;

import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.example.administrator.maizuofilm.BaseFragment;
import com.example.administrator.maizuofilm.R;
import com.example.administrator.maizuofilm.hot.HotFragment;
import com.example.administrator.maizuofilm.movie.adapter.MovieFragmentPagerAdapter;
import com.example.administrator.maizuofilm.movie.view.CustomScrollView;
import com.example.administrator.maizuofilm.movie.view.NetworkImageHolderView;
import com.example.administrator.maizuofilm.notshow.NotShowFragment;
import com.example.administrator.maizuofilm.recommend.fragment.RecommendFragment;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler2;
import in.srain.cube.views.ptr.PtrFrameLayout;

/**
 * Created by Administrator on 2016/7/11.
 */
public class MovieFragment extends BaseFragment {
    private static final float PERCENTAGE_TO_SHOW_TITLE_AT_TOOLBAR = 0.9f;
    private static final float PERCENTAGE_TO_HIDE_TITLE_DETAILS = 0.3f;
    @InjectView(R.id.imageview_bar_movie)
    ImageView imageviewBarMovie;
    @InjectView(R.id.scrollview_movie)
    CustomScrollView scrollviewMovie;


    private static final int ALPHA_ANIMATIONS_DURATION = 200;
    @InjectView(R.id.tablayout_body_movie)
    TabLayout tablayoutBodyMovie;
    @InjectView(R.id.viewpager_body_movie)
    ViewPager viewpagerBodyMovie;
    @InjectView(R.id.convenientBanner)
    ConvenientBanner convenientBanner;
    @InjectView(R.id.tablayout_bar_movie)
    TabLayout tablayoutBarMovie;
    @InjectView(R.id.appbar_movie)
    AppBarLayout appbarMovie;
    @InjectView(R.id.ptr)
    PtrClassicFrameLayout ptr;
    private List<Fragment> fragments;
    private MovieFragmentPagerAdapter pagerAdapter;
    private String[] images = {"http://img2.imgtn.bdimg.com/it/u=3093785514,1341050958&fm=21&gp=0.jpg",
            "http://img2.3lian.com/2014/f2/37/d/40.jpg",
            "http://d.3987.com/sqmy_131219/001.jpg",
            "http://img2.3lian.com/2014/f2/37/d/39.jpg",
            "http://www.8kmm.com/UploadFiles/2012/8/201208140920132659.jpg",
            "http://f.hiphotos.baidu.com/image/h%3D200/sign=1478eb74d5a20cf45990f9df460b4b0c/d058ccbf6c81800a5422e5fdb43533fa838b4779.jpg",
            "http://f.hiphotos.baidu.com/image/pic/item/09fa513d269759ee50f1971ab6fb43166c22dfba.jpg"
    };
    private List<String> networkImages;
    private int viewpagerPosition;
    private RecommendFragment recommendFragment;
    private HotFragment hotFragment;
    private NotShowFragment notShowFragment;
private int indexMore=0;//加载更多url  value
    private int indexRefresh=0;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_movie, null);
        ButterKnife.inject(this, view);
        setHasOptionsMenu(true);
        fragments = new ArrayList<>();
        addFragment();
        pagerAdapter = new MovieFragmentPagerAdapter(getFragmentManager(), fragments);
        viewpagerBodyMovie.setAdapter(pagerAdapter);
        tablayoutBodyMovie.setupWithViewPager(viewpagerBodyMovie);
        tablayoutBarMovie.setupWithViewPager(viewpagerBodyMovie);
        viewpagerBodyMovie.setOffscreenPageLimit(3);
        viewpagerBodyMovie.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                viewpagerPosition=position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        scrollviewMovie.smoothScrollTo(10, 10);
        ptr.setPtrHandler(new PtrDefaultHandler2() {
            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                switch (viewpagerPosition){
                    case 0:

                        break;
                    case 1:
                        break;
                    case 2:

                        indexMore+=10;
                        notShowFragment.getIndexMore(indexMore);
                        break;
                }
                frame.refreshComplete();
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                switch (viewpagerPosition){
                    case 0:

                        break;
                    case 1:
                        break;
                    case 2:
                        notShowFragment.getIndexRefresh(indexRefresh);

                        break;
                }
                frame.refreshComplete();
            }
        });
        scrollviewMovie.setOnScrollListener(new CustomScrollView.OnScrollListener() {
            @Override
            public void onScroll(int scrollY) {
                if (scrollY < convenientBanner.getHeight()) {
                    imageviewBarMovie.setAlpha(255);
                    imageviewBarMovie.setVisibility(View.VISIBLE);
                } else if (scrollY >= convenientBanner.getHeight()) {

                    float y = (scrollY - (float) convenientBanner.getHeight());
                    float p = (255 / (float) tablayoutBarMovie.getHeight());
                    imageviewBarMovie.setAlpha((int) (255 - p * y));
                    if (255 - p * y <= 0) {
                        if (imageviewBarMovie.getVisibility() == View.VISIBLE) {
                            imageviewBarMovie.setVisibility(View.INVISIBLE);
                        }
                        if (tablayoutBarMovie.getVisibility() == View.INVISIBLE) {
                            ObjectAnimator objectAnimator = ObjectAnimator
                                    .ofFloat(tablayoutBarMovie, "translationY", 0, 0, 100, 0)
                                    .setDuration(1000);
                            objectAnimator.start();
                        }
                    } else {
                        if (imageviewBarMovie.getVisibility() == View.INVISIBLE) {
                            imageviewBarMovie.setVisibility(View.VISIBLE);
                        }
                    }
                }
            }
        });

        //网络加载例子
        networkImages = Arrays.asList(images);
        convenientBanner.setPages(new CBViewHolderCreator<NetworkImageHolderView>() {
            @Override
            public NetworkImageHolderView createHolder() {
                return new NetworkImageHolderView();
            }
        }, networkImages);

//        if (appbarMovie != null)
//            appbarMovie.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
//                @Override
//                public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
//                }
//            });


        return view;
    }


    private void addFragment() {
         recommendFragment = new RecommendFragment();
        Bundle recommendBundle = new Bundle();
        recommendBundle.putString("title", "推荐");
        recommendFragment.setArguments(recommendBundle);

         hotFragment = new HotFragment();
        Bundle hotBundle = new Bundle();
        hotBundle.putString("title", "热映影片");
        hotFragment.setArguments(hotBundle);

         notShowFragment = new NotShowFragment();
        Bundle notShowBundle = new Bundle();
        notShowBundle.putString("title", "即将上映");
        notShowFragment.setArguments(notShowBundle);

        fragments.add(recommendFragment);
        fragments.add(hotFragment);
        fragments.add(notShowFragment);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setMenuVisibility(true);

//        getActivity().setActionBar();
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
//        super.onCreateOptionsMenu(menu, inflater);
        onPrepareOptionsMenu(menu);
        inflater.inflate(R.menu.actionbar_menu, menu);
        Log.i("tag", "完成");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }


}
